#include "gamepushbutton.h"
#include <QPixmap>
#include <QMouseEvent>
#include <QPropertyAnimation>

GamePushButton::GamePushButton(QWidget *parent) : QPushButton(parent)
{

}

GamePushButton::GamePushButton(QString imgNormal, QString imgPressed)
    : m_imgNormal(imgNormal)
    , m_imgPressed(imgPressed)
{
    //
    if(m_imgNormal != "")
    {
        QPixmap normalPix;
        bool ret = normalPix.load(m_imgNormal);
        if(ret)
        {
            QSize pSize(normalPix.width(), normalPix.height());
            this->setFixedSize(pSize);
            this->setStyleSheet("QPushButton{border: 0px}");
            this->setIcon(normalPix);
            this->setIconSize(pSize);
        }
    }
}


void GamePushButton::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        if(m_imgPressed != "")
        {
            QPixmap pressPix;
            bool ret = pressPix.load(m_imgPressed);
            if(ret)
            {
                QSize pSize(pressPix.width(), pressPix.height());
                this->setFixedSize(pSize);
                this->setStyleSheet("QPushButton{border: 0px}");
                this->setIconSize(pSize);
            }
        }
//        else
//        {

//        }
    }
    return QPushButton::mousePressEvent(e);
}

void GamePushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        if(m_imgNormal != "")
        {
            QPixmap normalPix;
            bool ret = normalPix.load(m_imgNormal);
            if(ret)
            {
                QSize pSize(normalPix.width(), normalPix.height());
                this->setFixedSize(pSize);
                this->setStyleSheet("QPushButton{border: 0px}");
                this->setIcon(normalPix);
                this->setIconSize(pSize);
            }
        }
    }
    return QPushButton::mouseReleaseEvent(e);
}

void GamePushButton::zoomUp()
{
    QPropertyAnimation *upAnimation = new QPropertyAnimation(this, "geometry");
    upAnimation->setDuration(500);
    upAnimation->setStartValue(QRect(this->x(), this->y() + 10, 100, 30));
    upAnimation->setEndValue(QRect(this->x(), this->y() +10, 100, 30));
    upAnimation->setEasingCurve(QEasingCurve::InOutBounce);

    upAnimation->start();
}

void GamePushButton::zoomDown()
{
    QPropertyAnimation *downAnimation = new QPropertyAnimation(this, "geometry");
    downAnimation->setDuration(500);
    downAnimation->setStartValue(QRect(this->x(), this->y() + 10, 100, 30));
    downAnimation->setEndValue(QRect(this->x(), this->y(), 100, 30));
    downAnimation->setEasingCurve(QEasingCurve::InOutBounce);

    downAnimation->start();
}

